package com.itheima.service.Impl;

import com.itheima.common.utils.POIUtils;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.vo.OrderSettingVO;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;

    /**
     * 根据日期修改预约人数
     * @param orderSetting
     */
    public void editNumberByOrderDate(OrderSetting orderSetting) {
        OrderSetting orderSetting1 = orderSettingMapper.findByOrderDate(orderSetting.getOrderDate());
        if (orderSetting1 == null) {
            orderSettingMapper.insert(orderSetting);
        } else {
            orderSetting1.setNumber(orderSetting.getNumber());
            orderSettingMapper.editNumberByOrderDate(orderSetting1);
        }

    }

    /**
     * 根据年月查询对应的预约设置信息
     *
     * @param month
     * @return
     */
    public ArrayList<OrderSettingVO> getOrderSettingByMonth(String month) {
        // 获取当月的开始日期和结束日期
        LocalDate beginDate = LocalDate.parse(month + "-01", DateTimeFormatter.ofPattern("yyyy-M-dd"));
        LocalDate endDate = beginDate.withDayOfMonth(beginDate.lengthOfMonth());

        ArrayList<OrderSetting> orderSettings = orderSettingMapper.getOrderSettingByMonth(beginDate, endDate);
        ArrayList<OrderSettingVO> orderSettingVOS = new ArrayList<>();
        for (OrderSetting orderSetting : orderSettings) {
            //获取日期中当前月第几天
            Integer date = orderSetting.getOrderDate().getDate();
            OrderSettingVO orderSettingVO = new OrderSettingVO(date, orderSetting.getNumber(), orderSetting.getReservations());
            orderSettingVOS.add(orderSettingVO);
        }

        return orderSettingVOS;

    }

    /**
     * 下载预约设置模板
     *
     * @param response
     * @param filename
     */
    public void downloadTemplate(HttpServletResponse response, String filename) {

        InputStream in = this.getClass().getClassLoader().getResourceAsStream("templates/ordersetting_template.xlsx");

        try {
            // 设置响应的 Content-Type 为 Excel 文件的 MIME 类型
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

            // 设置 Content-Disposition 头信息，指定文件名
            response.setHeader("Content-Disposition", "attachment; filename=" + filename);

            XSSFWorkbook excel = new XSSFWorkbook(in);

            ServletOutputStream out = response.getOutputStream();
            excel.write(out);

            out.close();
            excel.close();

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 上传预约设置数据
     * @param excelFile
     */
    public void upload(MultipartFile excelFile) {

        try {
            POIUtils.checkFile(excelFile);
            List<String[]> list = POIUtils.readExcel(excelFile);
            if (list != null && list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    String[] strings = list.get(i);
                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
                    Date orderDate = null;
                    orderDate = dateFormat.parse(strings[0]);
                    Integer number = Integer.parseInt(strings[1]);
                    OrderSetting orderSetting = new OrderSetting(orderDate, number);
                    orderSettingMapper.insert(orderSetting);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
